/**
 * Auteur : Dominique GUERIN
 * dominiquephDOTguerinATgmailDOT..
 * dominiqueDOTguerinATinseeDOTfr(ance)  
 * Remerciements: � Keith Brown pour ses deux livres sur la s�curit� sous Windows et ses diff�rents articles
 *                 et � Michel Barnett de Microsoft pour ses deux articles 
 *                ".NET Remoting Authentication and Authorization Sample"  et le code qui les accompagne
 *  Le code est utilisable, modifiable et redistribuable � volont� sous la condition de ne pas supprimer ces 7 lignes.
 */ 
package fr.doume.cli.authenticator;


//tomcat 5.5
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

//tomcat 6
//import org.apache.juli.logging.Log;
//import org.apache.juli.logging.LogFactory;

import java.util.Timer;
import java.util.TimerTask;
import java.lang.String;


import java.util.concurrent.ConcurrentHashMap;

public class HashMapWithTimeOut< K, V > extends ConcurrentHashMap< K, V >
{
  private static final long serialVersionUID = 1L;
  
  private static Log log = LogFactory.getLog(HashMapWithTimeOut.class);
  
  private int timeout = 10;
  
  public HashMapWithTimeOut( int timeout)
  {
    super();
    this.timeout = timeout;
  }
  public V put( K key, V value)
  {
    V result = super.put(key, value);
    Timer t = new Timer();
    t.schedule(new RemoveFromHashMap(key), timeout *1000); //Time out default 10s
    if (log.isDebugEnabled()) log.debug("A new RemoveFromHashMap");
    return result;
  }
  
  public V remove(Object key)
  {
    if (log.isDebugEnabled()) log.debug("Remove a key");
    return super.remove(key);
  }
  
  class RemoveFromHashMap extends TimerTask
  {
    private Object _key;
    public RemoveFromHashMap(Object key)
    {
      _key = key;  
    }

    public void run()
    {
      if (log.isDebugEnabled()) log.debug("Remove a key after a time out");
      remove(_key);      
    }
  }
}  
